package com.afs.electronicforensics.activity;


import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.Toast;

import com.afs.electronicforensics.activity.LoginActivity;
import com.afs.electronicforensics.activity.R;
import com.afs.electronicforensics.fragment.SignInFragment;
import com.afs.electronicforensics.utils.Constants;
import com.airbnb.lottie.LottieAnimationView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class IntroductoryActivity extends AppCompatActivity {
    ImageView logo,appName,splashImg;
    LottieAnimationView lottieAnimationView;

    SharedPreferences sp = null;
    boolean isUserExist = true;

    private static final int NUM_PAGES =3;
    Animation anim;

    private final int SPLASH_DISPLAY_LENGHT = 3000;  //延迟3秒
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_introductory);
        logo=findViewById(R.id.logo);
        appName=findViewById(R.id.app_name);
        splashImg=findViewById(R.id.img);
        lottieAnimationView=findViewById(R.id.lottie);

        //隐藏标题栏以及状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        isUserExist = sp.getBoolean("autoLogin",false);

        anim = AnimationUtils.loadAnimation(this,R.anim.o_n_anim);

        Runnable runnable =new Runnable() {
            @Override
            public void run() {
                if(isUserExist){

                    String name = sp.getString("ename", null);
                    String password = sp.getString("epwd", null);

                    if(name.trim()!=null&&password.trim()!=null){

                        String path = Constants.URL_ROOT+"user/login"+"?name=" + name +
                                "&password=" + password;
                        new doLogin().execute(path);
                    } else{
                        Intent intent = new Intent();
                        intent.setClass(IntroductoryActivity.this, LoginActivity.class);
                        startActivity(intent);
                        IntroductoryActivity.this.finish();
                    }
                }else{
                    Intent intent = new Intent();
                    intent.setClass(IntroductoryActivity.this, LoginActivity.class);
                    startActivity(intent);
                    IntroductoryActivity.this.finish();
                }
            }
        };

        splashImg.animate().translationY(-2500).setDuration(1000).setStartDelay(2000);
        appName.animate().translationY(2000).setDuration(1000).setStartDelay(2000);
        logo.animate().translationY(2200).setDuration(1000).setStartDelay(2000);
        lottieAnimationView.animate().translationY(2000).setDuration(1000).setStartDelay(2000).withEndAction(runnable);

    }



    //判断登录
    private class doLogin extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] params) {

            String path = params[0].toString();

            try {
                //以get方式提交整个路径
                URL url = new URL(path);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");//这个应该都知道是干嘛的，默认是GET
                conn.setConnectTimeout(5000);
                conn.setDoInput(true);
                conn.setDoOutput(true);
                conn.setUseCaches(false);
                conn.setRequestProperty("Content-Type","application/json;charset=utf-8");
                conn.connect();

                if (conn.getResponseCode() == 200) {
                    InputStream is = conn.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    String str = br.readLine();
                    return str;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            if(o==null){

            }else{
                String info = o.toString().trim();
                try {
                    JSONObject jo = new JSONObject(info);
                    String data = jo.getString("data");
                    JSONObject jsonData = new JSONObject(data);

                    Constants.USER_ROOT.setUid(jsonData.getString("id"));
                    Constants.USER_ROOT.setUsername(jsonData.getString("name"));
                    Constants.USER_ROOT.setPassword(jsonData.getString("password"));
                    Constants.USER_ROOT.setPhone(jsonData.getString("phone"));
                    if(jsonData.getString("photo").trim() != null && !("").equals(jsonData.getString("photo"))){
                        Constants.USER_ROOT.setUserAvatar(jsonData.getString("photo"));
                    }else{
                        Constants.USER_ROOT.setUserAvatar(null);
                    }

                    //设置路径
                    Constants.USER_PATH.setUserPath(jsonData.getString("id"));

                    //跳转页面
                    Intent intent = new Intent();
                    intent.setClass(IntroductoryActivity.this, ShowActivity.class);
                    startActivity(intent);
                    IntroductoryActivity.this.finish();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    }


}